#include "button.h"

namespace igslib
{
    CGPIOButton::CGPIOButton()
    {
        name = "GPIOButton";
    }
    void CGPIOButton::Update()
    {
        if (gpio->GetMode() != IGPIO::mode::input)
        {
            press_count = 0;
            return;
        }

        if (gpio->GetState() == IGPIO::state::high)
        {
            ++press_count;
            if (press_count == 1)
            {
                for(auto& each:events)
                {
                    each->OnChangeState(this,false,true);
                }
            }
        }
        else
        {
            if (press_count > 0)
            {
                for(auto& each:events)
                {
                    each->OnChangeState(this,true,false);
                }
            }
            press_count = 0;
        }
    }
};
